package com.easemob.chat;

import com.easemob.chat.core.e;
import com.easemob.util.EMLog;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;

public class EMConversation
{
  private static final String TAG = "conversation";
  List<EMMessage> messages;
  private int unreadMsgCount = 0;
  private String username;
  private boolean isGroup = false;
  private EMContact opposite = null;
  long msgCount = 0L;

  public EMConversation(String paramString)
  {
    this.username = paramString;
    if (this.messages == null)
      this.messages = Collections.synchronizedList(new ArrayList());
    if (this.unreadMsgCount <= 0)
      this.unreadMsgCount = e.a().j(paramString);
  }

  public EMConversation(String paramString, boolean paramBoolean)
  {
    this.username = paramString;
    this.isGroup = paramBoolean;
    if (this.messages == null)
      this.messages = Collections.synchronizedList(new ArrayList());
    if (this.unreadMsgCount <= 0)
      this.unreadMsgCount = e.a().j(paramString);
  }

  public EMConversation(String paramString, List<EMMessage> paramList, boolean paramBoolean, Long paramLong)
  {
    this.username = paramString;
    this.isGroup = paramBoolean;
    if (this.messages == null)
      this.messages = Collections.synchronizedList(paramList);
    if (this.unreadMsgCount <= 0)
      this.unreadMsgCount = e.a().j(paramString);
    this.msgCount = paramLong.longValue();
  }

  public void addMessage(EMMessage paramEMMessage)
  {
    if (paramEMMessage.getChatType() == EMMessage.ChatType.GroupChat)
      this.isGroup = true;
    if (this.messages.size() > 0)
    {
      EMMessage localEMMessage1 = (EMMessage)this.messages.get(this.messages.size() - 1);
      if ((paramEMMessage.getMsgId() != null) && (localEMMessage1.getMsgId() != null) && (paramEMMessage.getMsgId().equals(localEMMessage1.getMsgId())))
        return;
    }
    int i = 0;
    Iterator localIterator = this.messages.iterator();
    while (localIterator.hasNext())
    {
      EMMessage localEMMessage2 = (EMMessage)localIterator.next();
      if (localEMMessage2.getMsgId().equals(paramEMMessage.getMsgId()))
      {
        i = 1;
        break;
      }
    }
    if (i == 0)
    {
      this.messages.add(paramEMMessage);
      if ((paramEMMessage.direct == EMMessage.Direct.RECEIVE) && (paramEMMessage.unread))
      {
        this.unreadMsgCount += 1;
        saveUnreadMsgCount(this.unreadMsgCount);
      }
    }
    this.msgCount += 1L;
  }

  void addMessage(EMMessage paramEMMessage, boolean paramBoolean)
  {
    if (paramEMMessage.getChatType() == EMMessage.ChatType.GroupChat)
      this.isGroup = true;
    if (this.messages.size() > 0)
    {
      EMMessage localEMMessage1 = (EMMessage)this.messages.get(this.messages.size() - 1);
      if ((paramEMMessage.getMsgId() != null) && (localEMMessage1.getMsgId() != null) && (paramEMMessage.getMsgId().equals(localEMMessage1.getMsgId())))
        return;
    }
    int i = 0;
    Iterator localIterator = this.messages.iterator();
    while (localIterator.hasNext())
    {
      EMMessage localEMMessage2 = (EMMessage)localIterator.next();
      if (localEMMessage2.getMsgId().equals(paramEMMessage.getMsgId()))
      {
        i = 1;
        break;
      }
    }
    if (i == 0)
    {
      this.messages.add(paramEMMessage);
      this.msgCount += 1L;
      if ((paramEMMessage.direct == EMMessage.Direct.RECEIVE) && (paramEMMessage.unread) && (paramBoolean))
      {
        this.unreadMsgCount += 1;
        saveUnreadMsgCount(this.unreadMsgCount);
      }
    }
  }

  void saveUnreadMsgCount(final int paramInt)
  {
    EMChatManager.getInstance().msgCountThreadPool.submit(new Runnable()
    {
      public void run()
      {
        e.a().a(EMConversation.this.username, paramInt);
      }
    });
  }

  void deleteUnreadMsgCountRecord()
  {
    EMChatManager.getInstance().msgCountThreadPool.submit(new Runnable()
    {
      public void run()
      {
        e.a().k(EMConversation.this.username);
      }
    });
  }

  public int getUnreadMsgCount()
  {
    if (this.unreadMsgCount < 0)
      this.unreadMsgCount = 0;
    return this.unreadMsgCount;
  }

  public void resetUnsetMsgCount()
  {
    this.unreadMsgCount = 0;
    saveUnreadMsgCount(0);
  }

  public void resetUnreadMsgCount()
  {
    this.unreadMsgCount = 0;
    saveUnreadMsgCount(0);
  }

  public int getMsgCount()
  {
    return this.messages.size();
  }

  public int getAllMsgCount()
  {
    return (int)this.msgCount;
  }

  public EMMessage getMessage(int paramInt)
  {
    if (paramInt >= this.messages.size())
    {
      EMLog.e("conversation", "outofbound, messages.size:" + this.messages.size());
      return null;
    }
    EMMessage localEMMessage = (EMMessage)this.messages.get(paramInt);
    if ((localEMMessage != null) && (localEMMessage.unread))
    {
      localEMMessage.unread = false;
      if (this.unreadMsgCount > 0)
      {
        this.unreadMsgCount -= 1;
        saveUnreadMsgCount(this.unreadMsgCount);
      }
    }
    return localEMMessage;
  }

  public List<EMMessage> loadMoreMsgFromDB(String paramString, int paramInt)
  {
    Object localObject = new ArrayList();
    localObject = e.a().b(this.username, paramString, paramInt);
    this.messages.addAll(0, (Collection)localObject);
    Iterator localIterator = ((List)localObject).iterator();
    while (localIterator.hasNext())
    {
      EMMessage localEMMessage = (EMMessage)localIterator.next();
      EMChatManager.getInstance().addMessage(localEMMessage, false);
    }
    return localObject;
  }

  public List<EMMessage> loadMoreGroupMsgFromDB(String paramString, int paramInt)
  {
    List localList = e.a().a(this.username, paramString, paramInt);
    this.messages.addAll(0, localList);
    Iterator localIterator = localList.iterator();
    while (localIterator.hasNext())
    {
      EMMessage localEMMessage = (EMMessage)localIterator.next();
      EMChatManager.getInstance().addMessage(localEMMessage, false);
    }
    return localList;
  }

  public EMMessage getMessage(String paramString)
  {
    for (int i = this.messages.size() - 1; i >= 0; i--)
    {
      EMMessage localEMMessage = (EMMessage)this.messages.get(i);
      if (localEMMessage.msgId.equals(paramString))
      {
        if (localEMMessage.unread)
        {
          localEMMessage.unread = false;
          if (this.unreadMsgCount > 0)
          {
            this.unreadMsgCount -= 1;
            saveUnreadMsgCount(this.unreadMsgCount);
          }
        }
        return localEMMessage;
      }
    }
    return null;
  }

  public List<EMMessage> getAllMessages()
  {
    return this.messages;
  }

  public int getMessagePosition(EMMessage paramEMMessage)
  {
    try
    {
      Iterator localIterator = this.messages.iterator();
      while (localIterator.hasNext())
      {
        EMMessage localEMMessage = (EMMessage)localIterator.next();
        if (paramEMMessage.getMsgId().equals(localEMMessage.getMsgId()))
          return this.messages.indexOf(localEMMessage);
      }
    }
    catch (Exception localException)
    {
    }
    return -1;
  }

  public String getUserName()
  {
    return this.username;
  }

  public void removeMessage(String paramString)
  {
    EMLog.d("conversation", "remove msg from conversation:" + paramString);
    for (int i = this.messages.size() - 1; i >= 0; i--)
    {
      EMMessage localEMMessage = (EMMessage)this.messages.get(i);
      if (localEMMessage.msgId.equals(paramString))
      {
        if (localEMMessage.unread)
        {
          localEMMessage.unread = false;
          if (this.unreadMsgCount > 0)
          {
            this.unreadMsgCount -= 1;
            saveUnreadMsgCount(this.unreadMsgCount);
          }
        }
        this.messages.remove(i);
        if (this.msgCount > 0L)
          this.msgCount -= 1L;
        e.a().b(paramString);
      }
    }
  }

  public boolean getIsGroup()
  {
    return this.isGroup;
  }

  public boolean isGroup()
  {
    return this.isGroup;
  }

  public void setGroup(boolean paramBoolean)
  {
    this.isGroup = paramBoolean;
  }

  public EMMessage getLastMessage()
  {
    if (this.messages.size() == 0)
      return null;
    return (EMMessage)this.messages.get(this.messages.size() - 1);
  }

  public void clear()
  {
    this.messages.clear();
    this.unreadMsgCount = 0;
    e.a().k(this.username);
  }

  public void setExtField(String paramString)
  {
    e.a().a(this.username, this.isGroup, paramString);
  }

  public String getExtField()
  {
    return e.a().i(this.username, this.isGroup);
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.EMConversation
 * JD-Core Version:    0.6.1
 */